# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime

from . import tools

import logging
_logger = logging.getLogger(__name__)

class CrSafetyLostCreditType(models.Model):

    _name = "cr.safety.lost.credit.type"
    _description = "cr.safety.lost.credit.type"

    name = fields.Char()
    job_type = fields.Selection([
        ('staff', '职工'),
        ('manager', '干部')], default='manager')



class CrSafetyLostCredit(models.Model):
    _name = "cr.safety.lost.credit"
    _description = "cr.safety.lost.credit"

    name = fields.Char()

    date = fields.Date('date')
    date_year = fields.Char( 'date_year' )
    date_year_name = fields.Char( 'date_year_name' )
    date_month = fields.Char( 'date_month' )
    date_month_name = fields.Char( 'date_month_name' )
    num_by_year = fields.Integer()

    # date_year = fields.Char( 'date_year',  compute ='_compute_date_month', store=True)
    # date_year_name = fields.Char( 'date_year_name',  compute ='_compute_date_month')
    # date_month = fields.Char( 'date_month',  compute ='_compute_date_month', store=True)
    # date_month_name = fields.Char( 'date_month_name',  compute ='_compute_date_month')
    # num_by_year = fields.Integer()

    # @api.depends('date_month')
    # def _compute_date_month(self ):
    #     for rec in self:
    #         rec.date_year = datetime.datetime.strftime(rec.date, "%Y") + '-01-01'
    #         rec.date_year_name = datetime.datetime.strftime(rec.date, "%Y")
    #         rec.date_month = datetime.datetime.strftime(rec.date, "%Y-%m") + '-01'
    #         rec.date_month_name = datetime.datetime.strftime(rec.date, "%Y-%m")

  
    employee_id = fields.Many2one('hr.employee', 'employee_id'  )
    employee_name = fields.Char( 'employee_name',  related ='employee_id.name')
    employee_barcode = fields.Char( 'employee_barcode',  related ='employee_id.barcode')
    employee_job_type = fields.Selection([
        ('staff', '职工'),
        ('manager', '干部')], related= 'employee_id.job_type')

    employee_job_id = fields.Many2one('hr.job', 'employee_job_id', related='employee_id.job_id' )

    department_id = fields.Many2one(
        'hr.department', 'department_id', 
        related='employee_id.department_id', store=True)


    #employee_dept_id = fields.Many2one('hr.department', 'employee_dept_id', related='employee_id.dept_id' )


    type_id = fields.Many2one('cr.safety.lost.credit.type', 'type_id'  )
    helper_id = fields.Many2one('hr.employee', 'helper_id'  )

    start_date = fields.Date('start_date')
    end_date = fields.Date('end_date')


    reason = fields.Text()
    solution = fields.Text()
    check_result = fields.Text()
    check_notes = fields.Text()

    state = fields.Selection([
        ('draft', '草稿'),
        ('submit', '提交'),
        ('approve', '审批'),
        ('done', '评价'),
        
        ('cancel', '审批未通过')], default='draft')

    @api.model
    def create( self, values ):
        rec = super( CrSafetyLostCredit, self).create(values)
        rec.date_year = datetime.datetime.strftime(rec.date, "%Y") + '-01-01'
        rec.date_year_name = datetime.datetime.strftime(rec.date, "%Y")
        rec.date_month = datetime.datetime.strftime(rec.date, "%Y-%m") + '-01'
        rec.date_month_name = datetime.datetime.strftime(rec.date, "%Y-%m")
        return rec

    # TBDCODE, 被其他调用, type_id_ref 是 xml
    def find_or_create(self, date_month, employee_id, type_id_ref):
        type_id = self.env['ir.model.data'].xmlid_to_res_id(type_id_ref, True)
        domain = [
            ['date_month', '=', date_month],
            ['employee_id', '=', employee_id],
            ['type_id', '=',type_id],
        ]

        obj = self.search(domain)

        if obj:
            return obj

        vals = {
            'date':  date_month,
            'employee_id': employee_id,
            'type_id': type_id
        }

        obj = self.create(vals)
        obj.reason = obj.date_month_name + ' ' + obj.type_id.name
        return obj


